﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GabageFactory.Utiliy.Crypt;

namespace GabageFactory.Models
{
    public class StackWaterReposity : Reposity
    {
        public IEnumerable<stack_pool_daily_water_info> GetDailyPoolWaterInfo(int stackId, DateTime date)
        {
            var dailyWaters = from s in _dataContext.stack_pool_daily_water_infos
                             where stackId == s.stack_id && s.date == date.Date
                             select s;

            foreach (stack_pool_daily_water_info dailyWater in dailyWaters)
            {
                yield return dailyWater;
            }
        }

        public stack_monthly_phosphorus_fluorin GetWeeklyPhosphorusFluorin(int stackId, DateTime date)
        {
            DateTime weekDate = Tools.GetWeeklyDate(date);
            var info = from s in _dataContext.stack_monthly_phosphorus_fluorins
                       where stackId == s.stack_id && s.date == weekDate.Date
                              select s;

            if (info != null && info.Count() > 0)
            {
                return info.First();
            }
            else
            {
                return null;
            }
        }

        public stack_monthly_phosphorus_fluorin CreateMonthlyPhosphorusFluorin(stack_monthly_phosphorus_fluorin info)
        {
            _dataContext.stack_monthly_phosphorus_fluorins.InsertOnSubmit(info);
            SubmitChanges();
            return info;
        }

        public bool DeleteWeeklyPhosphorusFluorin(stack_monthly_phosphorus_fluorin info)
        {
            _dataContext.stack_monthly_phosphorus_fluorins.DeleteOnSubmit(info);
            return true;
        }

        public stack_monthly_phosphorus_fluorin EditMonthlyPhosphorusFluorin(stack_monthly_phosphorus_fluorin info)
        {
            SubmitChanges();
            return info;
        }

        public IDictionary<DateTime, stack_monthly_phosphorus_fluorin> QueryMonthlyPhosphorusFluorin(int stackId, DateTime dateFrom, DateTime dateTo)
        {
            var pfs = from s in _dataContext.stack_monthly_phosphorus_fluorins
                             where stackId == s.stack_id && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, stack_monthly_phosphorus_fluorin> infos = new Dictionary<DateTime, stack_monthly_phosphorus_fluorin>();
            foreach (stack_monthly_phosphorus_fluorin info in pfs)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public IDictionary<DateTime, stack_pool_daily_water_info> QueryPoolDailyWaterInfo(int stackId, int poolNo, DateTime dateFrom, DateTime dateTo)
        {
            var dailywater = from s in _dataContext.stack_pool_daily_water_infos
                             where stackId == s.stack_id && poolNo  == s.pool_no && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, stack_pool_daily_water_info> infos = new Dictionary<DateTime, stack_pool_daily_water_info>();
            foreach (stack_pool_daily_water_info info in dailywater)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public stack_pool_daily_water_info CreateDailyPoolWaterInfo(stack_pool_daily_water_info dailyPoolWaterInfo)
        {
            _dataContext.stack_pool_daily_water_infos.InsertOnSubmit(dailyPoolWaterInfo);
            SubmitChanges();
            return dailyPoolWaterInfo;
        }

        public void DeleteDailyPoolWaterInfos(int stackId, DateTime date)
        {
            var dailyPoolWaterInfos = from s in _dataContext.stack_pool_daily_water_infos
                        where s.stack_id == stackId && s.date == date.Date
                        select s;

            foreach (stack_pool_daily_water_info dailyPoolWaterInfo in dailyPoolWaterInfos)
            {
                _dataContext.stack_pool_daily_water_infos.DeleteOnSubmit(dailyPoolWaterInfo);
            }
            SubmitChanges();
        }

        public stack_daily_water_info GetDailyWaterInfo(int stackId, DateTime date)
        {
            var dailyWater = from s in _dataContext.stack_daily_water_infos
                             where stackId == s.stack_id && s.date == date.Date
                             select s;

            if (dailyWater != null && dailyWater.Count() > 0)
            {
                return dailyWater.First();
            }
            else
            {
                return null;
            }
        }

        public IDictionary<DateTime, stack_daily_water_info> QueryDailyWaterInfo(int stackId, DateTime dateFrom, DateTime dateTo)
        {
            var dailywater = from s in _dataContext.stack_daily_water_infos
                             where stackId == s.stack_id && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, stack_daily_water_info> infos = new Dictionary<DateTime, stack_daily_water_info>();
            foreach (stack_daily_water_info info in dailywater)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public stack_daily_water_info CreateWaterInfo(stack_daily_water_info waterInfo)
        {
            _dataContext.stack_daily_water_infos.InsertOnSubmit(waterInfo);
            SubmitChanges();
            return waterInfo;
        }

        public bool DeleteWaterInfo(stack_daily_water_info waterInfo)
        {
            _dataContext.stack_daily_water_infos.DeleteOnSubmit(waterInfo);
            return true;
        }

        public stack_daily_water_info EditWaterInfo(stack_daily_water_info waterInfo)
        {
            SubmitChanges();
            return waterInfo;
        }
    }
}